package com.zhb.parse;

import com.zhb.beans.Course;
import com.zhb.beans.SongList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: XianYvTest
 * @description:
 * @author: rainboz
 * @create: 2019-09-20 17:26
 **/
public class SongListParse {

    public static List<SongList> getData(String entity) {
        //获取的数据，存放在集合中
        List<SongList> data = new ArrayList<SongList>();
        //采用Jsoup解析
        Document doc = Jsoup.parse(entity);
        //获取html标签中的内容
        Elements elements = doc.select("div[class=songlist__item]");
        for (Element ele : elements) {
            String number = ele.select("div[class=songlist__number  songlist__number--top]").text();
            String rank = ele.select("div[class=songlist__rank]").text();
            String songName = ele.select("div[class=songlist__songname]").
                    select("span[class=songlist__songname_txt]").
                    select("a[class=js_song]").
                    attr("title");
            String mvUrl = ele.select("div[class=songlist__songname]").
                    select("span[class=songlist__songname_txt]").
                    select("a[class=songlist__cover album_name]").attr("href");
            String songnameTitle  = songName;
            String artist = ele.select("div[class=songlist__artist]").attr("title");
            String time  = ele.select("div[class=songlist__time]").text();

            //创建一个对象，这里可以看出，使用Model的优势，直接进行封装
            SongList songList = new SongList(number,rank,songName,mvUrl,songnameTitle,artist,time);
            data.add(songList);
            System.out.println("songList:======================= "+songList);
        }
        System.out.println("data=========================="+data);
        return data;
    }
}
